package com.cjxt.ams.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cjxt.ams.dao.RoleDao;
import com.cjxt.ams.dao.UserDao;
import com.cjxt.ams.domain.Role;
import com.cjxt.ams.domain.RoleDept;
import com.cjxt.ams.domain.RoleMenu;
import com.cjxt.ams.domain.User;
import com.cjxt.ams.service.RoleDeptService;
import com.cjxt.ams.service.RoleMenuService;
import com.cjxt.ams.service.RoleService;
import com.cjxt.ams.service.UserRoleService;
import com.cjxt.ams.utils.PageUtils;
import com.cjxt.ams.utils.R;
import com.cjxt.ams.utils.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.*;

import static org.apache.shiro.web.filter.mgt.DefaultFilter.roles;

/**
 * @author Jk_a
 * @version V1.0
 * @Description:角色管理
 * @className: roleController
 * @date 2021/11/24 17:00
 * @company:华勤技术股份有限公司
 * @copyright: Copyright (c) 2021
 */
@RestController
@RequestMapping("/role")
public class roleController extends AbstractController{

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private RoleDeptService roleDeptService;

    @Resource
    private RoleDao roleDao;

    @Resource
    private UserDao userDao;
    @Autowired
    private UserRoleService userRoleService;


    /**
     * @author Jk_a
     * @version V1.0
     * @Description:角色页面
     * @Param: params
     * @date 2021/11/24 17:30
     */
    @ResponseBody
    @RequestMapping("/pageList")
    public R list (@RequestParam Map<String,Object> params){
        PageUtils page = roleService.queryPage(params);
        return R.ok().put("page",page);
    }


    /**
     * @author Jk_a
     * @version V1.0
     * @Description:新增角色
     * @Param: role
     * @date 2021/11/24 17:40
     */
    @ResponseBody
    @RequestMapping("/add")
    //@RequiresPermissions("role:save")
    public R insertRole(Role role){

        //获取创建人
        role.setCreateBy(getUser().getUsername());
        //设置角色绑定菜单项中系统菜单
        List<Integer> roleMenuIdList = role.getMenuIdList();
        roleMenuIdList.add(1);
        role.setMenuIdList(roleMenuIdList);

        return roleService.insertRole(role);
    }
    /**
     * @author Jk_a
     * @version V1.0
     * @Description: 修改角色
     * @Param: role
     * @date 2021/11/24 18:00
     */
    @PostMapping("/edit")
    //@RequiresPermissions("role:update")
    public R update(Role role){
        //获取修改人
        role.setUpdateBy(getUser().getUsername());

        //设置角色绑定菜单项中系统菜单
        List<Integer> roleMenuIdList = role.getMenuIdList();
        if (roleMenuIdList.contains(1)){
            roleService.update(role);
        }else {
            roleMenuIdList.add(1);
            role.setMenuIdList(roleMenuIdList);
        }
        return roleService.update(role);
    }

    /**
     * @author Jk_a
     * @version V1.0
     * @Description: 给角色分配用户
     * @Param: userId,roleId
     * @date 2021/11/24 18:30
     */
    @RequestMapping("/assignAddUser")
    public Result<User> assignAddUser(Integer userId,Integer roleId){

        if (userId >= 1 && roleId >= 1){
            //给当前角色分配用户
            userRoleService.saveOrUpdate(userId,roleId);
            return Result.ok("给角色分配用户成功！");
        }
        return Result.error("角色分配用户失败!");
    }

    /**
     * @author Jk_a
     * @version V1.0
     * @Description: 根据用户id解除用户与角色的绑定
     * @Param: userId
     * @date 2021/11/24 19:00
     */
    @RequestMapping("/removeAssignUser")
    public Result removeAssignUser(@Param("userId") Integer userId){

        roleService.del(userId);
        return Result.ok("解除绑定成功！");
    }

    /**
     * @author Jk_a
     * @version V1.0
     * @Description:根据角色Id批量删除
     * @Param: roleId
     * @date 2021/11/25 09:30
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Result delete(@Param("roleId") Integer[] roleId){

            roleService.deleteBatch(roleId);
            return Result.ok("删除角色成功！");
    }


    /**
     * @author Jk_a
     * @version V1.0
     * @Description: 角色下拉框
     * @Param:
     * @date 2021/11/26 16:00
     */
    @GetMapping("/findRoleAll")
    public List<Role> findAll01(){
        List<Role> role01 = roleDao.selectList(null);
        return role01;
    }


    /**
     * @author Jk_a
     * @version V1.0
     * @Description: 条件查询
     * @Param: role
     * @date 2022/01/13 18:32
     */
    @RequestMapping(value = "/selectPage")
    public Map<String,Object> selectPage(Role role){
        IPage<Role> roleIPage = roleService.selectRole(role);
        Map<String,Object> map = new HashMap<>();
        map.put("count",roleIPage.getTotal());
        map.put("code",0);
        map.put("data",roleIPage.getRecords());
        return map;
    }

    //,produces = "application/json;charset=utf-8"

    /**
     * @author Jk_a
     * @version V1.0
     * @Description: 根据角色id查询菜单id列表
     * @Param: roleId
     * @date 2021/12/03 10:21
     */
    @ResponseBody
    @RequestMapping("/queryMenuIdList")
    //@RequiresPermissions("role:queryMenuIdList")
    public Result queryMenuList(Integer roleId){
        List<Integer> menuIdList = roleMenuService.queryMenuIdList(roleId);
        return Result.ok(menuIdList);
    }


}